/*
Copyright (c)2009 Seesmic, Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.seesmic.twhix
{
	public class IqStanza extends Stanza
	{
		public var from:String;
		public var to:String;
		public var type:String;
		public var query:XML;
		public var id:String;
		
		public function IqStanza(connection:XMPP, parent:Stanza=null)
		{
			super(connection, parent);
		}
		
		override public function fromXML(inxml:XML):void {
			
			this.xml = inxml;
			this.from = xml.@from;
			if(conn.fulljid) this.to = conn.fulljid.toString();
			this.type = xml.@type;
			this.id = xml.@id;
			if(xml.query) {
				this.query = xml.query[0];
			}
		}
		
		public override function makeReply():Stanza {
			setTo(from);
			setFrom(conn.fulljid.toString());
			for each(var stanza:Stanza in p) {
				stanza.makeReply()
			}
			return this;
		}
		
		public function setup(to:String, type:String, from:String=null):void {
			this.to = to;
			this.type = type;
			if(from) {
				this.from = from;
			} else {
				this.from = conn.fulljid;
			}
			
		}
		
		public function setTo(to:String):void {
			this.to = to;
		}
		
		public function setFrom(from:String):void {
			this.from = from;
		}
		
		public function setType(type:String):void {
			this.type = type;
		}
		
		public function setID(id:String=null):String {
			if(!id) {
				this.id = conn.newId();
			}
			return this.id;
		}
		
		public function setQuery(queryx:XML):void {
			query = queryx;
		}
		
		public override function render():void {
			if(!id) {
				setID();
			}
			xml = <iq />;
			if(to) {
				xml.@to = to;
			}
			xml.@from = conn.fulljid.toString();
			xml.@type = type;
			xml.@id = id;
	 
			if(query) {
				xml.appendChild(query);
			}
			
		}
		
		override public function noHandler():void {
			//if we don't handle and IQ packet, it means we don't support the query.
			if(type == 'get' || type == 'set') {
				var iq:XML = <iq type='error' />;
				iq.@to = xml.@from;
				iq.@from = conn.fulljid.toString();
				iq.@id = xml.@id;
				var found:Boolean = false;
				var queryns:String;
				for each(var sub:XML in xml.children()) {
					if(sub.localName() == 'query') {
						found = true;
						queryns = sub.namespace(); 
						break;
					}
				}
				if(found) {
					var query:XML = <query />;
					query.@xmlns = queryns;
					iq.appendChild(query);
				}
				var error:XML = <error type='cancel'><service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>;
				iq.appendChild(error);
				conn.send(iq.toString());
			}
		}
	}
}
